<template>
    <el-form ref="ruleFormRef" style="width: 100%" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon >
        <el-form-item label="类型" prop="type">
            <el-radio-group v-model="ruleForm.type">
                <el-radio value="资金风险及措施" >资金风险及措施</el-radio>
                <el-radio value="法律风险及措施" >法律风险及措施</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="描述" prop="title">
            <el-input v-if="type == 1 || type == 0" v-model="ruleForm.title" :rows="4" type="textarea" :autosize="{ minRows: 4 }" />
            <el-text v-else>{{ ruleForm.title }}</el-text>
        </el-form-item>
        <el-form-item label="措施" prop="measure">
            <el-input v-if="type == 2 || type == 0" v-model="ruleForm.measure" :rows="4" type="textarea" :autosize="{ minRows: 4 }" />
            <el-text v-else>{{ ruleForm.measure }}</el-text>
        </el-form-item>
        <!-- <el-form-item label="反馈" prop="effectiveness">
            <el-input v-model="ruleForm.effectiveness" :rows="4" type="textarea" :autosize="{ minRows: 4 }" />
        </el-form-item>
        <el-form-item label="开始日期" prop="start_date">
            <el-date-picker v-model="ruleForm.start_date" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="结束日期" prop="end_date">
            <el-date-picker v-model="ruleForm.end_date" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%;" />
        </el-form-item> -->
    </el-form>
    <el-divider />
    <div class="text-center">
        <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="loading"> 保存 </el-button>
    </div>
</template>
  
<script lang="ts" setup>
import { reactive, ref,watch } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import http from '../../../../util/request';
const loading = ref<boolean>(false)
const type = ref<number>(0)
interface RuleForm {
    id: number
    project_id:number
    type:string
    person_id:number|string
    title: string
    measure: string
    effectiveness:string
    start_date: string
    end_date: string
    status: number
}
const formSize = ref<ComponentSize>('large')
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>({
    id: 0,
    project_id:0,
    type:'',
    person_id:0,
    title: '/',
    measure: '/',
    effectiveness:'/',
    start_date: '',
    end_date: '',
    status: 0,
})

const onGetData = () => {
    if(!ruleForm.value.id){
        return;
    }
    http.get('/risk/'+ruleForm.value.id).then((response:any)=>{
        if(response.data.data){
            ruleForm.value = response.data.data
        }
    })
}

const props = defineProps<{
    data:any
    type:number
}>()

watch(props,()=>{
    ruleForm.value.id = props.data
    ruleForm.value.type = ''
    ruleForm.value.person_id = ''
    ruleForm.value.title = '/',
    ruleForm.value.measure = '/',
    ruleForm.value.effectiveness = '/',
    ruleForm.value.start_date = '',
    ruleForm.value.end_date = '',
    ruleForm.value.status =  0,
    type.value = props.type
    onGetData()
    
},{ immediate: true })

const rules = reactive<FormRules<RuleForm>>({
    title: [
        { required: true, message: '请输入描述', trigger: 'blur' },
    ],
    type: [
        { required: true, message: '请选择类型', trigger: 'blur' },
    ],
    person_id: [
        { required: true, message: '请选择责任人', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
    console.log(ruleForm.value)
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            loading.value = true
            let request:any = null
            if(ruleForm.value.id){
                request = http.patch('/risk',ruleForm.value)
            }else{
                request = http.put('/risk',ruleForm.value)
            }
            request.then((response:any)=>{
                loading.value = false
                if(response.data.code != 200){
                    ElMessage.error(response.data.msg);
                    return
                }
                onSave()
            })      
        } else {
            console.log('error submit!', fields)
        }
    })
}

const onSave = () => {
    emit('onSave')
}

const emit = defineEmits(['onSave'])
</script>
<style scoped>
.el-tab-pane{height: calc(100vh - 25vh);overflow-y: scroll;}
::v-deep .el-form-item .el-form-item__label {
  font-size: 16px; /* 或者你想要的任何大小 */
}
::v-deep .el-tabs__item{font-size: 18px !important;}
</style>
